//
//  UILabel+YJSuperExt.m
//  YJCategoriesGather
//
//  Created by YJHou on 2015/3/17.
//  Copyright © 2015年 侯跃军 houmanager@Hotmail.com. All rights reserved.
//

#import "UILabel+YJSuperExt.h"

@implementation UILabel (YJSuperExt)

- (void)autoHeightWithin:(CGFloat)maxHeight{
    self.numberOfLines = 0;
    CGSize fitsize;
    if (IOS7_bb) {
        NSStringDrawingOptions option = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
        NSDictionary *attributes = @{NSFontAttributeName:self.font};
        CGRect rectSize =[self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), MAXFLOAT)
                                                 options:option
                                              attributes:attributes
                                                 context:nil];
        fitsize = CGSizeMake(rectSize.size.width, maxHeight);
    }
    else
    {
        fitsize= [self.text
                  sizeWithFont:self.font
                  constrainedToSize:CGSizeMake(INT_MAX, maxHeight)
                  lineBreakMode:self.lineBreakMode];
        
    }
    CGRect frame = self.frame;
    //fitsize.width = fitsize.width < self.frame.size.width?self.frame.size.width:fitsize.width;
    frame.size = fitsize;
    self.frame = frame;
}

+ (instancetype)labelWithText:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font {
    UILabel *label = [UILabel new];
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    return label;
}
@end
